<?php
// PHPwritEnglish by CodeBlock
// Released Under CC-GNU GPL.
// See http://creativecommons.org/licenses/GPL/2.0/ for more information.

$version = "1.0 beta";
$prompt = "computer,";
echo "Welcome to PHPwritEnglish v$version\n";
echo "Enter a command starting with \"$prompt ____\"\n\n";

// Define variables for later in the script:
$selectedfile = "*NOT SELECTED*";

// Call askinput() from below:
askinput(); // display the input form for the first time

// Create a new file
if(input("create a file")){ // user wants to make a file
	echo "Ok, I'll make a file. What would you like me to call it?\n--> ";
	$newfilename = fgets(STDIN);
	$newfilename = chop($newfilename); // strip newline from input
	$makefile = touch($newfilename); // actually make the file
if($makefile){ // if success
	file_put_contents($newfilename,"<?php\n");
	echo "I have created a new file, and named it $newfilename. I have also written the starting <?php tag in it :)\n";
askinput();
} else { // something failed.
	echo "I had a problem creating $newfilename. Please try again.\n"; 
	askinput(); }
} // end file


if(input("close this program")){
	echo "Ok, I'll shut down PHPwritEnglish. Have a nice day!\n";
	die();
}

if(input("tell me if this is working")){
	echo "Yeah, it's working :) \n";
askinput();
}

if(input("select a file")){
	echo "Ok, go ahead and select a file to write to.\n--> ";
	
	$selectedfile = fgets(STDIN);
	$selectedfile = chop($selectedfile);
	if(file_exists($selectedfile)){
		echo "Cool. I've selected $selectedfile to use to write to.\n";
	} else {
	echo "I'm terribly sorry, but the file you want me to write to does not exist. Try creating it with \"$prompt create a file\" and THEN running this again.\n";
	}
askinput();
}

if(input("what is the current file")){
echo "You are currently writing to the file $selectedfile\n";
echo "If you would like to change, just say \"$prompt let me select a file\"\n";
askinput();
}

if(input("what commands are available")){
	echo "Currently, the following commands are available:\n";
	echo "$prompt create a file | Create a file in the current directory.\n";
	echo "$prompt select a file | Lets the user select a file to use.\n";
	echo "$prompt what is the current file | Displays the currently selected file.\n";
	echo "$prompt close this program | Closes PHPwritEnglish.\n";
	echo "$prompt tell me if this is working | Just a debug test for the author of this program ;)\n";
	askinput();
}



// We're going to use preg_match for the complicated ones.
if(preg_match("/^$prompt define a variable called (.*) with the value (.*)/i",$command,$varstuff)){
	$varname = $varstuff[1];
	$varvalue = $varstuff[2];
	if($selectedfile != "*NOT SELECTED*"){
	file_put_contents($selectedfile,"\$$varname = $varvalue;\n",FILE_APPEND);
	askinput();
	} else { 
	echo "Woah. Sorry mate, but you have not selected a file to use.\n";
	askinput();
	}
}

if(input("finalize the selected file")){
if($selectedfile != "*NOT SELECTED*"){
	echo "Ok, I'm going to add the closing ?> tag to this file, and exit myself.\n";
	file_put_contents($selectedfile,"?>",FILE_APPEND);
} else {
	echo "You have not selected a file to work with. Sorry.\n";
	}
}

if(input("delete the selected file")){
	if($selectedfile != "*NOT SELECTED*"){
	echo "Are you absolutely sure you wanted to delete the selected file? ($selectedfile) (yes or no)\n";
	echo "--> ";
	$yesno = fgets(STDIN);
	$yesno = chop($yesno);
	if($yesno == "yes"){
		unlink($selectedfile);
		echo "I have deleted $selectedfile.\n";
		askinput();
	} elseif($selectedfile == "no"){
		echo "Ok, I have not deleted the selected file.\n";
		askprompt();
	}
} else { echo "No file is currently selected.\n"; askinput(); }
}

function askinput(){
	global $command;
	echo "> ";
	$command = fgets(STDIN);
	$command = trim($command);
	// $command = str_replace(Array(".","?"),"",$command);
}


function input($invar){
	global $command;
	global $prompt;
	return preg_match("/^$prompt $invar/i",$command);
}
